/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SettingsForm.java
 *
 * Created on Aug 2, 2011, 4:08:34 PM
 */

package redclient.ui;

import java.awt.Toolkit;
import java.io.IOException;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import redclient.RedmineCredentials;

/**
 *
 * @author Isaias
 */
public class SettingsForm extends javax.swing.JFrame {

    private ImageIcon exitIcon;
    
    private ImageIcon cancelIcon;
    
    private boolean isEditing = false;

    private boolean isAddingNew = false;

    private List<RedmineCredentials> credentials;

    private RedmineCredentials currentCredentials = null;

    private static SettingsForm instance;

    public static SettingsForm getInstance()
    {
        if ( instance == null )
        {
            instance = new SettingsForm();
        }
        return instance;
    }
    /** Creates new form SettingsForm */
    private SettingsForm() {
        initComponents();
        exitIcon = new ImageIcon(this.getClass().getResource("/redclient/ui/img/form_exit_icon.png"));
        cancelIcon = new ImageIcon(this.getClass().getResource("/redclient/ui/img/button_cancel.png"));
        setResizable(false);
        populateCredentials();
        setEnableFlag();
        this.setIconImage(Toolkit.getDefaultToolkit().getImage(
                                        Toolkit.getDefaultToolkit()
                                               .getClass()
                                               .getResource("/redclient/ui/img/sandclock.png")
                              ));
        this.setTitle("Redmine Instances");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        textRedmineUrl = new javax.swing.JTextField();
        lblRedmineUrl = new javax.swing.JLabel();
        lblUsername = new javax.swing.JLabel();
        lblPassword = new javax.swing.JLabel();
        lblProxyServer = new javax.swing.JLabel();
        btnAddNew = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        textUsername = new javax.swing.JTextField();
        textPassword = new javax.swing.JTextField();
        textProxyServer = new javax.swing.JTextField();
        lblProxyPort = new javax.swing.JLabel();
        textProxyPort = new javax.swing.JTextField();
        cmbRegisteredUrls = new javax.swing.JComboBox();
        btnSave = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        lblRefreshTime = new javax.swing.JLabel();
        spnrRefreshTime = new javax.swing.JSpinner();
        lblRegisteredInstances = new javax.swing.JLabel();
        lblInstanceName = new javax.swing.JLabel();
        textInstanceName = new javax.swing.JTextField();
        lblValidInput = new javax.swing.JLabel();
        chkActvive = new javax.swing.JCheckBox();
        lblDateFormat = new javax.swing.JLabel();
        textDateFormat = new javax.swing.JTextField();

        setLocationByPlatform(true);

        textRedmineUrl.setInputVerifier(new URLVerifier("Redmine URL", lblValidInput));
        textRedmineUrl.setNextFocusableComponent(textUsername);

        lblRedmineUrl.setText("Redmine URL");

        lblUsername.setText("Login");

        lblPassword.setText("Password");

        lblProxyServer.setText("Proxy Server");

        btnAddNew.setIcon(new javax.swing.ImageIcon(getClass().getResource("/redclient/ui/img/button_add.png"))); // NOI18N
        btnAddNew.setText("Add new");
        btnAddNew.setNextFocusableComponent(textInstanceName);
        btnAddNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddNewActionPerformed(evt);
            }
        });

        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/redclient/ui/img/button-edit.png"))); // NOI18N
        btnEdit.setText("Edit");
        btnEdit.setEnabled(false);
        btnEdit.setNextFocusableComponent(btnAddNew);
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        textUsername.setInputVerifier(new RequieredFieldValueVerifier("Login", lblValidInput));
        textUsername.setNextFocusableComponent(textPassword);

        textPassword.setNextFocusableComponent(textProxyServer);

        textProxyServer.setNextFocusableComponent(textProxyPort);

        lblProxyPort.setText("Proxy port");

        textProxyPort.setInputVerifier(new IntegerFieldValueVerifier("Proxy port", lblValidInput, true));
        textProxyPort.setNextFocusableComponent(spnrRefreshTime);

        cmbRegisteredUrls.setNextFocusableComponent(btnEdit);
        cmbRegisteredUrls.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmbRegisteredUrlsActionPerformed(evt);
            }
        });

        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/redclient/ui/img/button_ok.png"))); // NOI18N
        btnSave.setText("Save");
        btnSave.setEnabled(false);
        btnSave.setNextFocusableComponent(btnCancel);
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/redclient/ui/img/form_exit_icon.png"))); // NOI18N
        btnCancel.setText("Exit");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        lblRefreshTime.setText("Refresh cache (minutes)");

        spnrRefreshTime.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(15), Integer.valueOf(0), null, Integer.valueOf(5)));
        spnrRefreshTime.setEnabled(false);
        spnrRefreshTime.setNextFocusableComponent(chkActvive);

        lblRegisteredInstances.setText("Registered instances");

        lblInstanceName.setText("Instance name");

        textInstanceName.setInputVerifier(new RequieredFieldValueVerifier("Instance name", lblValidInput));
        textInstanceName.setNextFocusableComponent(textRedmineUrl);

        lblValidInput.setForeground(new java.awt.Color(255, 0, 51));

        chkActvive.setText("Active");
        chkActvive.setEnabled(false);
        chkActvive.setNextFocusableComponent(btnSave);

        lblDateFormat.setText("Date format");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(lblRefreshTime)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(spnrRefreshTime, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(36, 36, 36)
                                .addComponent(chkActvive))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(lblUsername)
                                .addGap(207, 207, 207)
                                .addComponent(lblPassword))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(textProxyServer, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lblProxyServer))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblProxyPort)
                                    .addComponent(textProxyPort, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                        .addComponent(textUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(textPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                        .addComponent(lblRedmineUrl)
                                        .addGap(18, 18, 18)
                                        .addComponent(textRedmineUrl, javax.swing.GroupLayout.DEFAULT_SIZE, 359, Short.MAX_VALUE))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(lblRegisteredInstances)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE)
                                        .addComponent(cmbRegisteredUrls, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(lblInstanceName)
                                        .addGap(53, 53, 53)
                                        .addComponent(textInstanceName, javax.swing.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)))
                                .addGap(9, 9, 9)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnEdit, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                            .addComponent(btnAddNew, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(lblValidInput)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(textDateFormat, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblDateFormat)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cmbRegisteredUrls, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblRegisteredInstances))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnAddNew, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblInstanceName)
                            .addComponent(textInstanceName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(20, 20, 20)
                                .addComponent(lblRedmineUrl))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(textRedmineUrl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(10, 10, 10)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblUsername)
                            .addComponent(lblPassword))
                        .addGap(11, 11, 11)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(textUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(textPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblProxyServer)
                            .addComponent(lblProxyPort))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(textProxyServer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(textProxyPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblRefreshTime)
                            .addComponent(spnrRefreshTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(chkActvive))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lblValidInput)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnCancel)
                            .addComponent(btnSave)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lblDateFormat)
                        .addGap(3, 3, 3)
                        .addComponent(textDateFormat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        if( isEditing || isAddingNew ){
            if( currentCredentials != null )
            {
                textInstanceName.setText(currentCredentials.getInstanceName());
                textRedmineUrl.setText(currentCredentials.getHost());
                textUsername.setText(currentCredentials.getUsername());
                textPassword.setText(currentCredentials.getPassword());
                textProxyServer.setText(currentCredentials.getProxyhost());
                textProxyPort.setText(Integer.toString(currentCredentials.getProxyport()));
                spnrRefreshTime.setValue(currentCredentials.getRefreshTime());
                chkActvive.setSelected(currentCredentials.isActive());
                textDateFormat.setText(currentCredentials.getDateFormat());
            }
            isEditing = false;
            isAddingNew = false;
            setEnableFlag();
            lblValidInput.setText(null);
        }
        else{
            this.dispose();
        }
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnAddNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddNewActionPerformed
        currentCredentials = (RedmineCredentials)cmbRegisteredUrls.getSelectedItem();
        textInstanceName.setText(null);
        textRedmineUrl.setText(null);
        textUsername.setText(null);
        textPassword.setText(null);
        textProxyServer.setText(null);
        textProxyPort.setText(null);
        spnrRefreshTime.setValue(Integer.valueOf(15));
        chkActvive.setSelected(true);
        textDateFormat.setText(null);
        isAddingNew = true;
        setEnableFlag();
    }//GEN-LAST:event_btnAddNewActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        currentCredentials = (RedmineCredentials)cmbRegisteredUrls.getSelectedItem();
        isEditing = true;
        setEnableFlag();
    }//GEN-LAST:event_btnEditActionPerformed
	
	private void cmbRegisteredUrlsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbRegisteredUrlsActionPerformed
        currentCredentials = (RedmineCredentials)cmbRegisteredUrls.getSelectedItem();
        textInstanceName.setText(currentCredentials.getInstanceName());
        textRedmineUrl.setText(currentCredentials.getHost());
        textUsername.setText(currentCredentials.getUsername());
        textPassword.setText(currentCredentials.getPassword());
        textProxyServer.setText(currentCredentials.getProxyhost());
        textProxyPort.setText(Integer.toString(currentCredentials.getProxyport()));
        textDateFormat.setText(currentCredentials.getDateFormat());
        spnrRefreshTime.setValue(currentCredentials.getRefreshTime());
        chkActvive.setSelected(currentCredentials.isActive());
    }//GEN-LAST:event_cmbRegisteredUrlsActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        if( isAddingNew )
        {
            try
            {
                RedmineCredentials rc = new RedmineCredentials();
                rc.setInstanceName(textInstanceName.getText());
                rc.setHost(textRedmineUrl.getText());
                rc.setUsername(textUsername.getText());
                rc.setPassword(textPassword.getText());
                rc.setProxyhost(textProxyServer.getText().length() > 0 ? textProxyServer.getText() : null);
                rc.setProxyport(textProxyPort.getText().length() > 0 ? Integer.parseInt(textProxyPort.getText()) : 0);
                rc.setRefreshTime((Integer)spnrRefreshTime.getValue());
                rc.setDateFormat(textDateFormat.getText());
                rc.setActive(chkActvive.isSelected());
                if( ! credentials.contains(rc) )
                {
                    credentials.add(rc);
                    RedmineCredentials.StoreCredentials(credentials);
                    JOptionPane.showMessageDialog(this, "Instance added successfully", "Instance added", JOptionPane.INFORMATION_MESSAGE);
                    isAddingNew = false;
                    setEnableFlag();
                    cmbRegisteredUrls.addItem(rc);
                }
                else
                {
                    JOptionPane.showMessageDialog(this, "There is an instance with the same name, please specify a different one",
                                                "Duplicated instance name", JOptionPane.ERROR_MESSAGE);
                }
            }
            catch(IOException ioe)
            {
                JOptionPane.showMessageDialog(this, "There was an error trying to save the new credentials, if the problem persists please contact your administrator",
                                            "Error saving credentials", JOptionPane.ERROR_MESSAGE);
                //TODO: Handle user friendly message
                ioe.printStackTrace(System.out);
            }
        }
        else
        {
            try
            {
                boolean duplicated = false;
                RedmineCredentials rc = currentCredentials;
                for(RedmineCredentials rc2 : credentials)
                {
                    if( rc2.getInstanceName().equals(textInstanceName.getText()) && rc2 != rc)
                    {
                        duplicated = true;
                        break;
                    }
                }
                if( ! duplicated )
                {
                    rc.setInstanceName(textInstanceName.getText());
                    rc.setHost(textRedmineUrl.getText());
                    rc.setUsername(textUsername.getText());
                    rc.setPassword(textPassword.getText());
                    rc.setProxyhost(textProxyServer.getText().length() > 0 ? textProxyServer.getText() : null);
                    rc.setProxyport(textProxyPort.getText().length() > 0 ? Integer.parseInt(textProxyPort.getText()) : 0);
                    rc.setRefreshTime((Integer)spnrRefreshTime.getValue());
                    rc.setActive(chkActvive.isSelected());
                    rc.setDateFormat(textDateFormat.getText());
                    RedmineCredentials.StoreCredentials(credentials);
                    JOptionPane.showMessageDialog(this, "Instance edited successfully", "Instance edited", JOptionPane.INFORMATION_MESSAGE);
                    isEditing = false;
                    setEnableFlag();
                }
                else
                {
                    JOptionPane.showMessageDialog(this, "There is an instance with the same name, please specify a different one",
                                                "Duplicated instance name", JOptionPane.ERROR_MESSAGE);
                }
            }
            catch(IOException ioe)
            {
                JOptionPane.showMessageDialog(this, "There was an error trying to save the credentials, if the problem persists please contact your administrator",
                                            "Error saving credentials", JOptionPane.ERROR_MESSAGE);
                //TODO: Handle user friendly message
                ioe.printStackTrace(System.out);
            }
        }
    }//GEN-LAST:event_btnSaveActionPerformed

    

    private void populateCredentials()
    {
        try
        {
            credentials = RedmineCredentials.getCredentials();
            for(RedmineCredentials rc : credentials)
            {
                cmbRegisteredUrls.addItem(rc);
            }
            if( credentials.size() > 0 )
            {
                btnEdit.setEnabled(true);
            }
        }
        catch(IOException ioe)
        {
            JOptionPane.showMessageDialog(this,
                    "Could not retrieve stored credentials, if problem persists please contact your administrator",
                    "Error retrieving credentials", JOptionPane.ERROR_MESSAGE);
            ioe.printStackTrace(System.out);
        }
    }

    public void setEnableFlag()
    {
        boolean enable = isEditing || isAddingNew;
        cmbRegisteredUrls.setEnabled(!enable && cmbRegisteredUrls.getItemCount() > 0);
        textInstanceName.setEnabled(enable); //
        textRedmineUrl.setEnabled(enable); //
        textUsername.setEnabled(enable); //
        textPassword.setEnabled(enable); //
        textProxyServer.setEnabled(enable); //
        textProxyPort.setEnabled(enable); //
        textDateFormat.setEnabled(enable); //
        spnrRefreshTime.setEnabled(enable);
        chkActvive.setEnabled(enable);
        btnEdit.setEnabled(!enable);
        btnAddNew.setEnabled(!enable);
        btnSave.setEnabled(enable);
        btnCancel.setIcon(enable ? cancelIcon : exitIcon);
        btnCancel.setText(enable ? "Cancel" : "Exit");
    }
    
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SettingsForm().setVisible(true);
            }
        });
    }


    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddNew;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnSave;
    private javax.swing.JCheckBox chkActvive;
    private javax.swing.JComboBox cmbRegisteredUrls;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblDateFormat;
    private javax.swing.JLabel lblInstanceName;
    private javax.swing.JLabel lblPassword;
    private javax.swing.JLabel lblProxyPort;
    private javax.swing.JLabel lblProxyServer;
    private javax.swing.JLabel lblRedmineUrl;
    private javax.swing.JLabel lblRefreshTime;
    private javax.swing.JLabel lblRegisteredInstances;
    private javax.swing.JLabel lblUsername;
    private javax.swing.JLabel lblValidInput;
    private javax.swing.JSpinner spnrRefreshTime;
    private javax.swing.JTextField textDateFormat;
    private javax.swing.JTextField textInstanceName;
    private javax.swing.JTextField textPassword;
    private javax.swing.JTextField textProxyPort;
    private javax.swing.JTextField textProxyServer;
    private javax.swing.JTextField textRedmineUrl;
    private javax.swing.JTextField textUsername;
    // End of variables declaration//GEN-END:variables

}
